package com.st.mall.order.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.st.mall.address.model.AddressModel;
import com.st.mall.address.service.IAddressService;
import com.st.mall.order.model.OrderInfoModel;
import com.st.mall.order.model.OrderProModel;
import com.st.mall.order.service.IOrderInfoService;
import com.st.mall.order.service.IOrderProService;
import com.st.mall.product.model.ProductModel;
import com.st.mall.product.service.IProductService;
import com.st.mall.scart.model.ScartModel;
import com.st.mall.user.model.UserModel;


@Controller
@RequestMapping("/orderpro")
public class OrderProController {

	@Autowired
	private IOrderProService service;
	@Autowired
	private IAddressService addrService;
	@Autowired
	private IOrderInfoService orInfoService;
	@Autowired
	private IProductService proService;
	
	@RequestMapping("/jiesuan")
	@ResponseBody
	public int settlement(String proCode,HttpSession session,String orderCode) {
		UserModel user = (UserModel) session.getAttribute("user");
		String custCode = user.getCode();
		
		ScartModel scart = new ScartModel();
		scart.setCustCode(custCode);
		scart.setProCode(proCode);
		scart.setStatus(1);
		
		int result = service.settlement(scart,orderCode);
		return result;
	}
	
	@RequestMapping(value = "/getsel",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String sel(OrderProModel orderProModel) {
		List<OrderProModel> list = service.selectAll(orderProModel);
		return new JSONArray(list).toString();
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public int delete(OrderProModel orderProModel) {	
		return service.delete(orderProModel);
	}
	
	@RequestMapping(value = "/get",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String selectId(OrderProModel orderProModel) {
		OrderProModel result = service.selectId(orderProModel);
		return new JSONObject(result).toString();
	}
	
	@RequestMapping("/insert")
	@ResponseBody
	public int insert(OrderProModel orderProModel,String code) {
		AddressModel address = new AddressModel();
		address.setUserCode(code);
		AddressModel tmp = addrService.selectId(address);
		if(tmp==null){
			return 0;
		}
		String addr = tmp.getAddr1Name()+"-"+tmp.getAddr2Name()+"-"+tmp.getAddr3Name()+"-"+tmp.getAddr4();
		OrderInfoModel model = new OrderInfoModel();
		model.setAddrCode(addr);
		model.setCustCode(code);
		String orderCode = orInfoService.add(model);
		ProductModel proModel = new ProductModel();
		ProductModel temp = proService.selectId(proModel.getCode());
		orderProModel.setPrice(temp.getPrice());
		orderProModel.setOrderCode(orderCode);
		return service.insert(orderProModel);
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public int update(OrderProModel orderProModel) {
		return service.update(orderProModel);
	}
	
	@RequestMapping(value = "/selectModel",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String selectModel(OrderProModel orderProModel) {
		System.out.println(orderProModel);
		List<OrderProModel> list = service.selectModel(orderProModel);
		for (OrderProModel orderProModel2 : list) {
			System.out.println(orderProModel2);
		}
		int result = service.selectCount(orderProModel);
		Map<String,Object> map = new HashMap<>();
		map.put("data",list);
		map.put("count",result);
		return new JSONObject(map).toString();
	}
}
